﻿/*
 * @author: wizardc
 */

using System.Collections.Generic;
using UnityEngine;

namespace Demo.WorldMap
{
    /// <summary>
    /// 场景静态渲染管理器
    /// </summary>
    public partial class SceneStaticRendererMgr
    {
        private static SceneStaticRendererMgr _instance;
        public static SceneStaticRendererMgr instance => _instance ??= new SceneStaticRendererMgr();

        private SceneStaticCameraCtl _defaultCameraCtl;
        private readonly List<SceneStaticCameraCtl> _cameraCtlList = new List<SceneStaticCameraCtl>();
        private readonly Dictionary<string, SceneStaticCameraCtl>  _cameraCtlMap = new Dictionary<string, SceneStaticCameraCtl>();
        
        private readonly List<ISceneStaticRenderer> _rendererList = new List<ISceneStaticRenderer>();
        
        private SceneStaticRendererMgr()
        {
        }

        /// <summary>
        /// 初始化
        /// </summary>
        public void Init()
        {
            var defaultCameraCtl = new GameObject("DefaultCameraCtl");
            _defaultCameraCtl = defaultCameraCtl.AddComponent<SceneStaticCameraCtl>();
            _defaultCameraCtl.camera = Camera.main;
            GameObject.DontDestroyOnLoad(defaultCameraCtl);
        }

        /// <summary>
        /// 添加一个相机控制器
        /// </summary>
        public void AddCameraCtl(SceneStaticCameraCtl cameraCtl)
        {
            if (_defaultCameraCtl != cameraCtl && !_cameraCtlList.Contains(cameraCtl) && !string.IsNullOrEmpty(cameraCtl.name))
            {
                _cameraCtlList.Add(cameraCtl);
                _cameraCtlMap.Add(cameraCtl.name, cameraCtl);
            }
        }

        /// <summary>
        /// 获取指定相机控制器
        /// </summary>
        public SceneStaticCameraCtl GetCameraCtl(string name)
        {
            if (_cameraCtlMap.ContainsKey(name))
            {
                return _cameraCtlMap[name];
            }
            return _defaultCameraCtl;
        }

        /// <summary>
        /// 移除一个相机控制器
        /// </summary>
        public void RemoveCameraCtl(SceneStaticCameraCtl cameraCtl)
        {
            if (_defaultCameraCtl != cameraCtl && _cameraCtlList.Contains(cameraCtl))
            {
                _cameraCtlList.Remove(cameraCtl);
                _cameraCtlMap.Remove(cameraCtl.name);
            }
        }

        /// <summary>
        /// 添加一个渲染器
        /// </summary>
        public void AddRender(ISceneStaticRenderer render)
        {
            if (!_rendererList.Contains(render))
            {
                _rendererList.Add(render);
            }
        }

        /// <summary>
        /// 移除一个渲染器
        /// </summary>
        public void RemoveRender(ISceneStaticRenderer render)
        {
            _rendererList.Remove(render);
        }
    }
}
